package cn.xmkeshe.back.impl;

import cn.xmkeshe.back.ILineServiceBack;
import cn.xmkeshe.dao.ILineDao;
import cn.xmkeshe.model.Line;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ILineServiceBackImpl implements ILineServiceBack {
  @Resource
  private ILineDao iLineDao;

  @Override
  @Transactional(readOnly = true)
  public Map<String, Object> findByName(int currentPage, int lineSize, String name) {
    Map<String,Object> map = new HashMap<String, Object>();
    map.put("data",iLineDao.findByName((currentPage-1)*lineSize, lineSize,name));
    map.put("count",iLineDao.getAllCount());
    return map;
  }

  //  增加前应该先判断路线是否存在
  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean insert(Line line) {
    if(iLineDao.findById(line.getLid())==null){
      return iLineDao.doCreate(line);
    }else {
      return false;
    }
  }

  @Override
  @Transactional(readOnly = true)
  public Map<String, Object> listSplit(int currentPage, int lineSize) {
    Map<String, Object> map = new HashMap<String,Object>();
    map.put("data",iLineDao.findAllSplit((currentPage-1)*lineSize, lineSize));
    map.put("count",iLineDao.getAllCount());
    return map;
  }

  @Override
  @Transactional(readOnly = true)
  public Line findById(String lid) {
    return iLineDao.findById(lid);
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public boolean edit(String lid,int status) {
    return iLineDao.doUpdateByLid(lid,status);
  }

  @Override
  @Transactional(readOnly = true)
  public List<Line> list() {
    return iLineDao.findAll();
  }


}
